package realtime.redis;

import redis.clients.jedis.Jedis;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by luoqifei on 17-4-17.
 */
public class WriteRedisDemo {
    private Jedis jedis;
    private static String fieldSample = "23.7612%s%s,";
    private static long timestamp=1493104167000L;
    public static int counter = 0;
    public WriteRedisDemo(String redisIp,int port){
        jedis = new Jedis(redisIp,port,99999999);
    }
    public static void flush(){
        WriteRedisDemo test = new WriteRedisDemo("127.0.0.1",6379);
        test.flushDb();
    }
    public void flushDb(){
        jedis.flushDB();
    }

    private long getTimeMinute(long unixTime){
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
        Date date = new Date(unixTime);
        String dateTime = df.format(date);
        try {
            return df.parse(dateTime).getTime()/1000;
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }
    public boolean setRecordWithTimeSlots(long times, String[] fName,String[] fValues,String fgId,String compoundId){
        //asset=fgId:compoundId
        long time = getTimeMinute(times);
        jedis.zadd("idx::"+fgId+":"+compoundId+"::"+time,times,""+times);
        counter = counter+1;
        for(int i=0;i<fName.length;i++){
            jedis.hset("dat::"+compoundId+"::"+fName[i]+"::"+time,fgId+":"+times,fValues[i]);
            counter = counter+1;
        }
        return true;
    }
    public boolean setRecord(long times, String[] fName,String[] fValues,String fgId,String compoundId){
        //asset=fgId:compoundId
        jedis.zadd("idx::"+fgId+":"+compoundId,times,""+times);
        counter++;
        for(int i=0;i<fName.length;i++){
            jedis.hset("dat::"+compoundId+"::"+fName[i],fgId+":"+times,fValues[i]);
            counter++;
        }
        return true;
    }
    public static void main(String args[]){
        flush();
//        int seconds = 10;
//        boolean withTimeSlots = false;
//        writeRecords(seconds,withTimeSlots);
        Jedis jedis = new Jedis("127.0.0.1",6379);
        System.out.println(jedis.info("Memory"));
        String arg[] = jedis.info("Memory").split("\n");
        System.out.println(Long.valueOf(arg[1].split(":")[1].trim()));
        System.out.println(Double.valueOf(arg[14].split(":")[1].trim()));
    }
    public static void writeRecords(int secs,boolean withTimeSlots){
        WriteRedisDemo test = new WriteRedisDemo("127.0.0.1",6379);
        int fgNum = 1;
        int assetNum = 60;
        int fieldNum = 47;
        long seconds = secs;
        long writeSpeed = 200;
        String[] fgIds = new String[fgNum];
        String[] compoundIds = new String[assetNum];
        String[] filedNames = new String[fieldNum];
        String[] filedValues = new String[fieldNum];
        for(int b=0;b<fgNum;b++){
            fgIds[b]="fieldGroupId_"+b;
        }
        for(int b=0;b<assetNum;b++){
            compoundIds[b]="compoundId_"+b;
        }
        for(int i=0;i<fieldNum;i++){
            filedNames[i] = "fieldName_"+i;
            filedValues[i] = String.format(fieldSample,String.valueOf(i+1),String.valueOf(i+1));
        }
        long time=System.currentTimeMillis();
        for(int b=0;b<fgNum;b++){
            for(int k=0;k<assetNum;k++){
                for(int j=0;j<seconds*writeSpeed;j++){
                    if(withTimeSlots){
                        test.setRecordWithTimeSlots(timestamp+j*5,filedNames,filedValues,fgIds[b],compoundIds[k]);
                    }else {
                        test.setRecord(timestamp+j*5,filedNames,filedValues,fgIds[b],compoundIds[k]);
                    }
                }
            }
        }
        System.out.println("had execute redis write "+counter+" .");
        System.out.println("costs "+(System.currentTimeMillis()-time)/1000+" seconds");
    }
    //delete sort set by range, delete hash time, cost count
    public static void removeData(){
        Jedis jedis = new Jedis("127.0.0.1",2999,60000);
        //each assert had 48000 record
        long start1=System.currentTimeMillis();
        for(int a=0;a<20;a++){
            jedis.zremrangeByRank("idx::fieldGroupIdValue_idField1Name-idField1Value_idField2Name-idField2Value"+a,0,1000);
        }
        System.out.println("del sort set,"+(System.currentTimeMillis()-start1)+" ms.");
        StringBuilder fields = new StringBuilder();
        for(int k=0;k<110000;k++){
            fields.append(k);
            if(k!=110000){
                fields.append(",");
            }
        }
        long start2=System.currentTimeMillis();
        for(int a=0;a<20;a++){
            jedis.hdel("val::fieldGroupIdValue_idField1Name-idField1Value_idField2Name-idField2Value" + a,fields.toString().split(",") );
        }
        System.out.println("del hash,"+(System.currentTimeMillis()-start2)+" ms.");

        long start3=System.currentTimeMillis();
        for(int a=0;a<20;a++){
            String aa[] =jedis.hkeys("time::fieldGroupIdValue_idField1Name-idField1Value_idField2Name-idField2Value"+a).toArray(new String[]{});
            jedis.hdel("time::fieldGroupIdValue_idField1Name-idField1Value_idField2Name-idField2Value" + a,aa );
        }
        System.out.println("del hash time,"+(System.currentTimeMillis()-start3)+" ms.");
    }
}
